﻿
package extremefx.storage {
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.system.LoaderContext;	

	/**
	 * @author Marcelo Volmaro
	 */
	public class DisplayObjectLoader extends QeuedItem {
		private var _loader:Loader;
		private var _do:DisplayObject;

		public function DisplayObjectLoader(pUrl:*, pContext:LoaderContext = null) {
			super(pUrl, pContext);
			_do = null;
		}

		override public function start():Boolean {
			var r:Boolean = super.start();
			if (r){
				_loader = new Loader();
				_addHandlers(_loader.contentLoaderInfo);
				_loader.load(_loadUrl, LoaderContext(_cnx));
			}
			
			return r;
		}

		override public function stop():Boolean {
			var r:Boolean = super.stop();
			if (r){
				try{
					_loader.close();
					_loader.unload();
				} catch(error:Error){}
			}
			
			return r;
		}
		
		override protected function _onLoadComplete():void{
			_do = _loader.content as DisplayObject;
		}
		
		public function get displayObject():DisplayObject { 
			return _do; 
		}
		
		override public function dispose():void {
			super.dispose();
			_loader = null;
		}
	}
}
